/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package t7;

import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.table.DefaultTableModel;
import java.util.Date;
import java.text.Format;
import java.text.SimpleDateFormat;

import java.io.*;
import javax.sound.sampled.*;

/**
 *
 * @author DUYKIENBK
 */
public class t7 extends Thread{
   private static menubox box=new menubox();
   private DefaultTableModel model = (DefaultTableModel)box.jTable1.getModel();

   
    @Override
   public void run() {
       String[] info={}; 
       int i=0;
       while(1==1) {

        try {
            ServerSocket srvr = new ServerSocket(1234);
            System.out.print("Server has connected!\n");
            Socket skt = srvr.accept();
            BufferedReader in = new BufferedReader(new
                InputStreamReader(skt.getInputStream()));
            System.out.print("Received string: '");

            while (!in.ready()) {}        
            info = in.readLine().split("&&");
            System.out.println(info[0]+":"+info[1]); // Read one line and output it
            System.out.print("\n");
            in.close();
            skt.close();
            srvr.close();
            if (!box.demon) {
                i=0;
                box.demon=true;
             }
            i++;
            addrow(i,info);
            playsound();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
       }
   }
    
    public void addrow(int index,String[] info) {
                Format  formatter = new SimpleDateFormat("hh:mm:ss a");
                Date date=new Date();                
                model.addRow(new Object[]{index,info[0],info[1],formatter.format(date)});
    }
    
    public void playsound() {
              try {
         // Open an audio input stream.
         File sound = new File(System.getProperty("user.dir")+"\\sound.wav");
         AudioInputStream audioIn = AudioSystem.getAudioInputStream(sound);
         // Get a sound clip resource.
         Clip clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioIn);
         clip.start();
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (LineUnavailableException e) {
         e.printStackTrace();
      }
   
    }
    

    
        /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(menubox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(menubox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(menubox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(menubox.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                box.setVisible(true);
                (new t7()).start();
            }
        });
    }
    
}

